package cn.buk.qms.service;

import cn.buk.common.JsonResult;
import cn.buk.tms.sc.SearchCustomerCriteria;

import java.util.ArrayList;
import java.util.List;

import cn.buk.tms.dto.*;
import cn.buk.tms.entity.misc.*;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Component;

@Component
public class CustomerServiceImpl extends BaseAbstractService implements CustomerService {


	@Override
	public List<Customer> getAllCustomers(int enterpriseId) {
		return customerDao.getAllCustomers(enterpriseId);
	}

	@Override
	public List<CustomerTravelZoneDto> searchCustomerTravelZones(int enterpriseId, SearchCustomerCriteria sc) {
		List<CustomerTravelZone> list = customerDao.searchCustomerTravelZones(enterpriseId, sc);

		List<CustomerTravelZoneDto> dtos = new ArrayList<>();
		for(CustomerTravelZone zone: list) {
			dtos.add(CustomerTravelZoneDto.createdBy(zone));
		}

		return dtos;
	}

	@Override
	public JsonResult saveCustomerTravelZone(int enterpriseId, String username, int customerId, int zoneId, int cityId, int mode) {
		int retCode = 0;
		if (mode == 4) {
			//移除
			retCode = customerDao.removeCustomerTravelZone(enterpriseId, username, customerId, zoneId, cityId);
		} else {
			//增加
			try {
				retCode = customerDao.addCustomerTravelZone(enterpriseId, username, customerId, zoneId, cityId);
			} catch (RuntimeException ex) {
				return JsonResult.createJsonResult(-100, ex.getMessage());
			}
		}
		return JsonResult.createJsonResult(retCode);
	}

  @Override
  public JsonResult saveCustomerTpHotel(int enterpriseId, String username, CustomerTpHotelDto dto) {
		CustomerTpHotel tpHotel = new CustomerTpHotel();
		BeanUtils.copyProperties(dto, tpHotel);

		Customer c = customerDao.getCustomerById(dto.getCustomerId());
		if (c.getEnterpriseId() != enterpriseId) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpHotel.setCustomer(c);

		CustomerPosition cp = customerDao.getCustomerPositionById(dto.getPositionId());
		if (cp.getCustomer().getId() != c.getId()) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpHotel.setCustomerPosition(cp);

		try {
			int retCode = customerDao.saveCustomerTpHotel(enterpriseId, username, tpHotel);

			return JsonResult.createJsonResult(retCode);
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

	@Override
	public List<CustomerTpHotelDto> searchCustomerTpHotels(int enterpriseId, SearchCustomerCriteria sc) {
		List<CustomerTpHotel> tpHotels = customerDao.searchCustomerTpHotels(enterpriseId, sc);
		List<CustomerTpHotelDto> dtos = new ArrayList<>();
		for(CustomerTpHotel tpHotel: tpHotels) {
			dtos.add(CustomerTpHotelDto.createdBy(tpHotel));
		}
		return dtos;
	}

	@Override
	@Secured({RoleInfo.ROLE_ADMIN, RoleInfo.ROLE_DATA_ADMIN})
	public JsonResult deleteCustomerTpHotel(int enterpriseId, String username, int customerId, int id) {
		int retCode = customerDao.deleteCustomerTpHotel(enterpriseId, username, customerId, id);

		return JsonResult.createJsonResult(retCode);
	}

  @Override
  public JsonResult saveCustomerTpFlight(int enterpriseId, String username, CustomerTpFlightDto dto) {
		if (dto.getFlyTime() == 0) {
			dto.setCabinClassAfter(null);
		}

		CustomerTpFlight tpFlight = new CustomerTpFlight();
		BeanUtils.copyProperties(dto, tpFlight);

		Customer c = customerDao.getCustomerById(dto.getCustomerId());
		if (c.getEnterpriseId() != enterpriseId) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpFlight.setCustomer(c);

		CustomerPosition cp = customerDao.getCustomerPositionById(dto.getPositionId());
		if (cp.getCustomer().getId() != c.getId()) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpFlight.setCustomerPosition(cp);

		try {
			int retCode = customerDao.saveCustomerTpFlight(enterpriseId, username, tpFlight);

			return JsonResult.createJsonResult(retCode);
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

	@Override
	public List<CustomerTpFlightDto> searchCustomerTpFlights(int enterpriseId, SearchCustomerCriteria sc) {
		List<CustomerTpFlight> tpFlights = customerDao.searchCustomerTpFlights(enterpriseId, sc);
		List<CustomerTpFlightDto> dtos = new ArrayList<>();
		for(CustomerTpFlight tpFlight: tpFlights) {
			dtos.add(CustomerTpFlightDto.createdBy(tpFlight));
		}
		return dtos;
	}

  @Override
  public JsonResult deleteCustomerTpFlight(int enterpriseId, String username, int customerId, int id) {
		int retCode = customerDao.deleteCustomerTpFlight(enterpriseId, username, customerId, id);

		return JsonResult.createJsonResult(retCode);
  }

  @Override
  public JsonResult saveCustomerTpCar(int enterpriseId, String username, CustomerTpCarDto dto) {
		CustomerTpCar tpCar = new CustomerTpCar();
		BeanUtils.copyProperties(dto, tpCar);

		Customer c = customerDao.getCustomerById(dto.getCustomerId());
		if (c.getEnterpriseId() != enterpriseId) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpCar.setCustomer(c);

		CustomerPosition cp = customerDao.getCustomerPositionById(dto.getPositionId());
		if (cp.getCustomer().getId() != c.getId()) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpCar.setCustomerPosition(cp);

		VehicleGroupInfo vehicleGroupInfo = enterpriseDao.getVehicleGroupInfoById(dto.getVehicleGroupId());
		if (vehicleGroupInfo.getEnterpriseId() != enterpriseId) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpCar.setVehicleGroup(vehicleGroupInfo);

		try {
			int retCode = customerDao.saveCustomerTpCar(enterpriseId, username, tpCar);

			return JsonResult.createJsonResult(retCode);
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

	@Override
	public JsonResult deleteCustomerTpCar(int enterpriseId, String username, int customerId, int id) {
		int retCode = customerDao.deleteCustomerTpCar(enterpriseId, username, customerId, id);

		return JsonResult.createJsonResult(retCode);
	}

	@Override
	public List<CustomerTpCarDto> searchCustomerTpCars(int enterpriseId, SearchCustomerCriteria sc) {
		List<CustomerTpCar> tpCars = customerDao.searchCustomerTpCars(enterpriseId, sc);
		List<CustomerTpCarDto> dtos = new ArrayList<>();
		for(CustomerTpCar tpCar: tpCars) {
			dtos.add(CustomerTpCarDto.createdBy(tpCar));
		}
		return dtos;
	}

  @Override
  public JsonResult saveCustomerTpTrain(int enterpriseId, String username, CustomerTpTrainDto dto) {
		CustomerTpTrain tpTrain = new CustomerTpTrain();
		BeanUtils.copyProperties(dto, tpTrain);

		Customer c = customerDao.getCustomerById(dto.getCustomerId());
		if (c.getEnterpriseId() != enterpriseId) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpTrain.setCustomer(c);

		CustomerPosition cp = customerDao.getCustomerPositionById(dto.getPositionId());
		if (cp.getCustomer().getId() != c.getId()) {
			throw new RuntimeException("Data is not consistent.");
		}
		tpTrain.setCustomerPosition(cp);


		try {
			int retCode = customerDao.saveCustomerTpTrain(enterpriseId, username, tpTrain);

			return JsonResult.createJsonResult(retCode);
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

	@Override
	public JsonResult deleteCustomerTpTrain(int enterpriseId, String username, int customerId, int id) {
		int retCode = customerDao.deleteCustomerTpTrain(enterpriseId, username, customerId, id);

		return JsonResult.createJsonResult(retCode);
	}

	@Override
	public List<CustomerTpTrainDto> searchCustomerTpTrains(int enterpriseId, SearchCustomerCriteria sc) {
		List<CustomerTpTrain> tpTrains = customerDao.searchCustomerTpTrains(enterpriseId, sc);
		List<CustomerTpTrainDto> dtos = new ArrayList<>();
		for(CustomerTpTrain tpTrain: tpTrains) {
			dtos.add(CustomerTpTrainDto.createdBy(tpTrain));
		}
		return dtos;
	}

  @Override
  public List<LoginInfoDto> searchCustomerLoginInfos(final int enterpriseId, SearchCustomerCriteria sc) {
    List<User> users = customerDao.searchCustomerLoginInfos(enterpriseId, sc);

    List<LoginInfoDto> dtos = new ArrayList<>();
    for(User user: users) {
			LoginInfoDto dto = getCustomerEmployeeLoginInfo(null, user);
    	dtos.add(dto);
		}

		return dtos;
  }


	@Override
	@Secured({RoleInfo.ROLE_ADMIN, RoleInfo.ROLE_DATA_ADMIN})
	public JsonResult saveCustomerDepartmentApprover(int enterpriseId, int userId, int customerId, int departmentId, int approverId, int roleType) {
		try {
			return JsonResult.createJsonResult(customerDao.saveCustomerDepartmentApprover(enterpriseId, userId, customerId, departmentId, approverId, roleType));
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
	}

  @Override
  public JsonResult deleteCustomerDepartmentApprover(int userId, int approverId) {
		try {
			return JsonResult.createJsonResult(customerDao.deleteCustomerDepartmentApprover(userId, approverId));
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

  @Override
  public JsonResult saveCustomerSmsMode(int enterpriseId, int userId, int customerId, int smsMode) {
		try {
			return JsonResult.createJsonResult(customerDao.saveCustomerSmsMode(enterpriseId, userId, customerId, smsMode));
		} catch (RuntimeException ex) {
			return JsonResult.createJsonResult(-100, ex.getMessage());
		}
  }

}
